< previous page page_518 next page >

Page 518
CONSTRAINT clause, you separate the CONSTRAINT clause with a comma and enclose the column names within parentheses. Mixing these two formats can lead to frustration when you are trying to debug an SQL script!
Tip.gif
Think of it this way. In the case of a single-column CONSTRAINT, these are additional qualifiers of the column; the constraint belongs within the column definition. A multicolumn CONSTRAINT, however, is a standalone definition that is not an extension of any one column definition. For this reason, multicolumn constraints are treated as if they are on an equal level with a column definition. They stand alone in the column list.

Using the FOREIGN KEY_REFERENCES Relationship
The most powerful of the CONSTRAINT formats is the FOREIGN KEY_REFERENCES format. This format is used to establish relationships between tables. Commonly, a FOREIGN KEY relationship is established between a small table containing a list of valid column entries (usually called a validation table) and another table. The second table usually has a column defined with the same name as the primary key column in the validation table. By establishing a foreign key relationship between the two files, you can enforce a database rule that says the only valid entries in a given table are those values that already exist in the primary key column of the validation table. Once again, you are using the database engine to store data integrity rules. This reduces your volume of Visual Basic code and increases database integrity.
Let's use the script from Listing 13.11 (SQLVB04.SQV) to create a foreign key relationship. You already have a table definedJobsTable. This is an excellent example of a validation table. It has few fields and has a single column defined as the primary key. Now let's add another tablethe EmpsTable. This table holds basic information about employees, including their respective job titles. Listing 13.12 shows modifications to SQLVB04.SQV that include the definition of the EmpsTable data table.
Type.gifListing 13.12. Adding a PRIMARY KEY CONSTRAINT to the EmpsTable.
//
// sqlvb04.sqv - Test CONSTRAINT SQL keyword
//
// create a database
dbMake sqlvb04.mdb;
// create jobs title table
CREATE TABLE JobsTable
   (JobTitle TEXT (20) CONSTRAINT PKJobTitle PRIMARY KEY,

 
< previous page page_518 next page >

If you like this book, buy it!